<template>
  <view class="g-page">
    <!-- 头部 -->
    <view class="m-navbar">
      <view class="logowrap">
        <image class="logo" src="/static/images/logo.png"></image>
        <text>新鲜 . 亲民 . 快捷</text>
      </view>
      <view class="search">
        <view>
          <u-icon @click="Scanning" name="scan" color="#fff" size="34"></u-icon>
        </view>
        <u-search
          :showAction="false"
          placeholder="搜索商品"
          v-model="keyword"
        ></u-search>
        <view>
          <u-icon @click="personal" name="account" color="#fff" size="34"></u-icon>
        </view>
      </view>
    </view>
    <!-- 轮播图 -->
    <swiper class="m-swiper" indicator-dots autoplay circular>
      <swiper-item v-for="item in swiperList" :key="item.id">
        <image :src="item.imgUrl"></image>
      </swiper-item>
    </swiper>
    <!-- 入口导航 -->
    <view class="m-nav">
      <template v-for="item in navList">
        <navigator
          :key="item.id"
          url="/pages/category/index"
          open-type="switchTab"
          hover-class="none"
          @click="categoryNav({id: item.id})"
        >
          <image :src="item.icon" mode="scaleToFill" />
          <view class="name">{{ item.name }}</view>
        </navigator>
      </template>
    </view>
    <!-- 人气推荐 -->
    <view class="m-hot m-section">
      <view class="hot" v-for="item in hotList" :key="item.id">
        <navigator
          :key="item.id"
          @click="newHot(item.id)"
          open-type="navigate"
          hover-class="none"
          class="wrapNav"
        >
          <view class="title">
            {{ item.title }}
            <text>{{ item.alt }}</text>
          </view>
          <view class="pics" v-for="goods in item.pictures" :key="goods">
            <image :src="goods" mode="scaleToFill" />
          </view>
        </navigator>
      </view>
    </view>
    <!-- 新鲜好物 -->
    <view class="m-fresh m-section">
      <view class="title">
        <text class="text">{{ "新鲜好物" }}</text>
        <text class="more">{{ "更多>>" }}</text>
      </view>
      <view class="wrap">
        <navigator
          v-for="item in freshtList"
          :key="item.id"
          url="/pages/goods/index"
          open-type="navigate"
          hover-class="none"
          class="wrapNav"
        >
          <image :src="item.picture" mode="scaleToFill" />
          <view class="name">{{ item.name }}</view>
          <view class="price">{{ item.price }}</view>
        </navigator>
      </view>
    </view>
    <!-- 热门品牌 -->
    <view class="m-brand m-section">
      <view class="title">
        <text class="text">{{ "热门品牌" }}</text>
        <text class="more">{{ "更多>>" }}</text>
      </view>
      <view class="wrap">
        <navigator
          v-for="item in brandList"
          :key="item.id"
          url="/pages/goods/index"
          open-type="switchTab"
          hover-class="none"
          class="wrapNav"
          @click="tips"
        >
          <image :src="item.picture" mode="scaleToFill" />
          <view class="name">{{ item.name }}</view>
          <view class="desc">{{ item.desc }}</view>
        </navigator>
      </view>
    </view>
    <!-- 热门专题 -->
    <view class="m-topic m-section">
      <view class="title">
        <text class="text">{{ "热门专题" }}</text>
        <text class="more">{{ "更多>>" }}</text>
      </view>
      <view class="wrap">
        <navigator
          v-for="item in topicList"
          :key="item.id"
          url="/pages/goods/index"
          open-type="switchTab"
          hover-class="none"
          class="wrapNav"
          @click="tips"
        >
          <image class="pic" :src="item.cover" mode="scaleToFill" />
          <view class="name">{{ item.title }}</view>
          <view class="price">{{ item.lowestPrice }}元起</view>
          <view class="info">
            <view class="collect">
              <u-icon name="heart" color="#666" size="12"></u-icon>
              {{ item.collectNum }}
            </view>
            <view class="collect">
              <u-icon name="eye" color="#666" size="12"></u-icon>
              {{ item.viewNum }}
            </view>
          </view>
        </navigator>
      </view>
    </view>
    <!-- 猜你喜欢 -->
    <view class="m-guess">
      <view class="caption">
        <image src="/static/images/bubble.png" mode="widthFix" class="c-left" />
        <text>猜你喜欢</text>
        <image
          src="/static/images/bubble.png"
          mode="widthFix"
          class="c-right"
        />
      </view>
      <view class="guesswrap">
        <navigator
          v-for="item in guesswrapList"
          :key="item.id"
          url="/pages/goods/index"
          open-type="navigate"
          hover-class="none"
          class="wrapNav"
        >
          <image :src="item.picture" mode="scaleToFill" />
          <view class="name">{{ item.name }}</view>
          <view class="price">{{ item.price }}</view>
        </navigator>
      </view>
    </view>
    <!-- 底部提示 -->
    <view
      class="tips"
      v-if="guesswrapList.length && guesswrapList.length === counts"
      >我也是有底线的~</view
    >
  </view>
</template>

<script>
export default {
  data() {
    return {
      keyword: "",
      // 轮播图列表
      swiperList: [],
      // 入口导航列表
      navList: [],
      // 人气推荐
      hotList: [],
      // 新鲜好物
      freshtList: [],
      // 热门品牌
      brandList: [],
      // 热门专题
      topicList: [],
      // 猜你喜欢
      guesswrapList: [],
      counts: 0,
      queryParams: {
        page: 1,
        pageSize: 20,
      }
    };
  },
  onLoad() {
    this.getSwiperList();
    this.getNavList();
    this.getHotList();
    this.getFreshtList();
    this.getBrandList();
    this.getTopicList();
    this.getGuesswrapList();
  },
  // 监听下拉的处理函数
  async onPullDownRefresh() {
    await Promise.all([
      this.getSwiperList(),
      this.getNavList(),
      this.getHotList(),
      this.getFreshtList(),
      this.getBrandList(),
      this.getTopicList(),
      this.getGuesswrapList(),
    ]);

    // 收起下拉刷新
    uni.stopPullDownRefresh();
  },
  // 触底钩子事件
  onReachBottom() {
    if (this.guesswrapList.length < this.counts) {
      // 页面+1
      this.queryParams.page++;
      // 重新请求下一页数据
      this.getGuesswrapList();
    } else {
      uni.showToast({
        title: "没有更多数据了",
        icon: "none",
      });
    }
  },
  methods: {
    async getSwiperList() {
      const res = await uni.$u.http.get("/home/banner");
      this.swiperList = res.data.result;
    },
    async getNavList() {
      const res = await uni.$u.http.get("/home/category/mutli");
      this.navList = res.data.result;
    },
    async getHotList() {
      const res = await uni.$u.http.get("/home/hot/mutli");
      this.hotList = res.data.result;
    },
    async getFreshtList() {
      const res = await uni.$u.http.get("/home/new");
      this.freshtList = res.data.result;
    },
    async getBrandList() {
      const res = await uni.$u.http.get("/home/brand");
      this.brandList = res.data.result;
    },
    async getTopicList() {
      const res = await uni.$u.http.get("/home/special");
      this.topicList = res.data.result;
    },
    async getGuesswrapList() {
      const res = await uni.$u.http.get("/home/goods/guessLike", {
        params: this.queryParams,
      });
      this.guesswrapList.push(...res.data.result.items);
      this.counts = res.data.result.counts;
    },
    // 提示
    tips() {
      uni.showToast({
        title: "下个版本才支持呦~",
        icon: "none",
      });
    },
    categoryNav(categoryLIst) {
      this.$store.commit('category/updateCategoryLIst', categoryLIst);
    },
    // 推荐页
    newHot(id) {
      const obj = {
        1: "preference",
        2: "inVogue",
        3: "oneStop",
      };
      if (id === "4") {
        return this.tips();
      }
      uni.navigateTo({
        url: `/pages/recommend/index?class=${obj[id]}`,
      });
    },
    // 个人信息
    personal() {
      uni.navigateTo({
        url: "/pages/my/profile",
      });
    },
    // 扫码
    Scanning() {
      uni.scanCode({
        success: function (res) {
          console.log("条码类型：" + res.scanType);
          console.log("条码内容：" + res.result);
        },
      });
    },
  },
};
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
.g-page {
  display: flex;
  flex-direction: column;
  background: #f7f7f8;
  width: 100%;
  height: 100%;

  .m-navbar {
    position: fixed;
    top: 0;
    width: 100%;
    padding: 20px 0 12rpx;
    overflow: hidden;
    background-color: #00bf97;
    z-index: 10;

    .logowrap {
      display: flex;
      height: 66rpx;
      margin-bottom: 24rpx;
      align-items: center;

      text {
        margin: 2rpx 0 0 20rpx;
        padding-left: 20rpx;
        font-size: 26rpx;
        color: #fff;
        border-left: 1px solid #fff;
      }
    }
    .logo {
      width: 166rpx;
      height: 39rpx;
      margin-left: 30rpx;
    }
    .search {
      display: flex;
    }
  }
  .m-swiper {
    margin-top: 210rpx;
    width: 750rpx;
    height: 340rpx;
    image {
      width: 750rpx;
      height: 340rpx;
    }
  }
  .m-nav {
    display: flex;
    flex-wrap: wrap;
    text-align: center;
    image {
      width: 100rpx;
      height: 100rpx;
      margin: 12rpx 25rpx;
    }
    .name {
      color: #666;
      font-size: 26rpx;
    }
  }
  .m-section {
    margin: 20rpx;
    background: #fff;
    border-radius: 10rpx;
    overflow: hidden;
  }
  .m-hot {
    display: flex;
    flex-wrap: wrap;
    .hot {
      display: flex;
      flex-direction: column;
      width: 50%;
      height: 254rpx;
      border-top: 1rpx solid #eee;
      .title {
        justify-content: flex-start;
        padding: 24rpx 20rpx 0;
        font-size: 32rpx;
        color: #262626;
        position: relative;

        text {
          font-size: 24rpx;
          color: #7f7f7f;
          margin-left: 18rpx;
          width: 168rpx;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }
      }
      .pics {
        display: flex;
        flex: 1;
        padding: 15rpx 20rpx;
        image {
          width: 100%;
          height: 160rpx;
        }
      }
    }
  }
  .title {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    .text {
      padding: 24rpx 0 0 24rpx;
      font-size: 32rpx;
      color: #262626;
    }
    .more {
      padding-right: 24rpx;
      font-size: 26rpx;
      color: #7f7f7f;
    }
  }
  .wrap {
    display: flex;
    .wrapNav {
      width: 25%;
      padding: 15rpx 20rpx 20rpx;
      line-height: 1;
      text-align: center;
      image {
        width: 126rpx;
        height: 126rpx;
      }
      .name {
        margin: 20rpx 0 16rpx;
        font-size: 28rpx;
        width: 136rpx;
        color: #262626;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      .price {
        line-height: 1;
        text-align: left;
        font-size: 26rpx;
        color: #cf4444;
      }
    }
  }
  .desc {
    font-size: 24rpx;
    color: #999;
    width: 140rpx;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .m-topic {
    .wrap {
      display: flex;
      .wrapNav {
        width: 50%;
        padding: 15rpx 20rpx 20rpx;
        line-height: 1;
        text-align: center;
        .pic {
          width: 325rpx;
          height: 164rpx;
        }
        .name {
          width: 325rpx;
        }
        .price {
          margin-top: 16rpx;
          font-size: 24rpx;
          color: #999;
        }
        .info {
          display: flex;
          flex-wrap: nowrap;
          font-size: 22rpx;
          margin-top: 16rpx;
          color: #666;
          .collect {
            display: flex;
            margin-right: 10rpx;
          }
        }
      }
    }
  }
  .m-guess {
    .caption {
      display: flex;
      justify-content: center;
      line-height: 1;
      padding: 36rpx 0 40rpx;
      font-size: 32rpx;
      color: #262626;
      image {
        width: 25rpx;
        margin: auto 0;
      }
      text {
        margin: 0 10rpx;
      }
    }
    .guesswrap {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      padding: 0 20rpx;
      .wrapNav {
        width: 340rpx;
        padding: 24rpx 0rpx 20rpx;
        margin-bottom: 20rpx;
        border-radius: 10rpx;
        overflow: hidden;
        background-color: #fff;
        image {
          width: 100%;
          height: 260rpx;
        }
        .name {
          height: 75rpx;
          margin: 10rpx 0;
          font-size: 28rpx;
          color: #262626;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .price {
          line-height: 1;
          padding-top: 4rpx;
          color: #cf4444;
          font-size: 26rpx;
        }
      }
    }
  }
  .tips {
    line-height: 80rpx;
    text-align: center;
    color: #999;
    font-size: 24rpx;
  }
}
</style>
